package com.sansi.bleFloodlight.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.a1anwang.okble.client.scan.BLEScanResult;
import com.a1anwang.okble.client.scan.DeviceScanCallBack;
import com.a1anwang.okble.client.scan.OKBLEScanManager;
import com.alibaba.android.arouter.facade.annotation.Route;

import com.my.httpapi.api.annotation.Layout;
import com.my.httpapi.api.baseUtils.LogUtils;
import com.my.httpapi.api.baseUtils.Toast;
import com.my.httpapi.api.baseView.BaseActivity;
import com.sansi.bleFloodlight.R;
import com.sansi.bleFloodlight.adapter.ConnectLightAdapter;
import com.sansi.bleFloodlight.adapter.SearchLightResultAdapter;
import com.sansi.bleFloodlight.ble.ScanRecordUtil;
import com.sansi.bleFloodlight.utils.RouterPath;

import java.util.ArrayList;
import java.util.List;

@Layout(R.layout.layout_activity_add_group)
@Route(path = RouterPath.addGroupPath)
public class AddGroupActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private SearchLightResultAdapter searchLightResultAdapter;
    private TextView btnNext;

    private List<Boolean> selectList = new ArrayList<>();
    private List<BLEScanResult> newList = new ArrayList<>();
    private ArrayList<BLEScanResult> bleDeviceChooseList = new ArrayList<>();
    private OKBLEScanManager scanManager;
    private boolean isSelectAll = false;

    @Override
    protected void initView() {
        btnNext = findViewById(R.id.btn_next);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(linearLayoutManager);
        searchLightResultAdapter = new SearchLightResultAdapter();
        recyclerView.setAdapter(searchLightResultAdapter);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        btnNext.setOnClickListener(view -> {
            boolean isSelect = false;
            for (int i = 0; i < selectList.size(); i++) {
                if (selectList.get(i)) {
                    isSelect = true;
                    bleDeviceChooseList.add(newList.get(i));
                }
            }
            if (!isSelect) {
                Toast.show("Please select a lamp");
                return;
            }
          //接下去的逻辑待定
        });
        //ble相关
        OKBle();
        searchOKBle();
    }

    @Override
    protected void destory() {

    }

    @Override
    protected String showTitle() {
        return "Add Group";
    }

    @Override
    protected boolean statusBarIsBlack() {
        return false;
    }

    @Override
    protected boolean showLeftArrow() {
        return true;
    }


    private void OKBle() {
        scanManager = new OKBLEScanManager(this);
        scanManager.setScanCallBack(scanCallBack);
    }

    private void searchOKBle() {
        scanManager.startScan();
    }

    private DeviceScanCallBack scanCallBack = new DeviceScanCallBack() {
        @Override
        public void onBLEDeviceScan(BLEScanResult device, int rssi) {
            //过滤掉相同的mac地址
            for (int i = 0; i < newList.size(); i++) {
                if (device.getMacAddress().equals(newList.get(i).getMacAddress())) {
                    return;
                }
            }
            String advertisingStr = ScanRecordUtil.bytesToHex(device.getAdvertisingData());
            if (advertisingStr.contains("5A69") && (advertisingStr.contains("DAE92165"))) {
                newList.add(device);
                selectList.add(false);
                searchLightResultAdapter.setNewData(newList, selectList);
            }
        }

        @Override
        public void onFailed(int code) {
            switch (code) {
                case DeviceScanCallBack.SCAN_FAILED_BLE_NOT_SUPPORT:
                    Toast.show("该设备不支持BLE");
                    break;
                case DeviceScanCallBack.SCAN_FAILED_BLUETOOTH_DISABLE:
                    Toast.show("请打开手机蓝牙");
                    break;
                case DeviceScanCallBack.SCAN_FAILED_LOCATION_PERMISSION_DISABLE:
                    Toast.show("请授予位置权限以扫描周围的蓝牙设备");
                    break;
                case DeviceScanCallBack.SCAN_FAILED_LOCATION_PERMISSION_DISABLE_FOREVER:
                    Toast.show("位置权限被您永久拒绝,请在设置里授予位置权限以扫描周围的蓝牙设备");
                    break;
            }
        }

        @Override
        public void onStartSuccess() {
            newList.clear();
            LogUtils.e(" scan:" + "开始扫描");
        }
    };
}
